package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
	"store/utils/jwt"
	"store/utils/response"
)

// JWTCheck 验证 jwt中间件
func JWTCheck() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		jwtObj := jwt.NewJwt()
		if jwtClaims, err := jwtObj.ParserToken(ctx); err != nil {
			response.Result(http.StatusUnauthorized, gin.H{}, err.Error(), ctx)
			ctx.Abort()
			return
		} else {
			uid := cast.ToInt64(jwtClaims.UserID)
			ctx.Set("uid", uid)
			/************检查是否有权限访问***************/
			/*allow, err := service.AllowCurrentPath(ctx, uid)
			if err != nil {
				response.Result(http.StatusForbidden, gin.H{}, err.Error(), ctx)
				ctx.Abort()
				return
			}
			if !allow {
				response.Result(http.StatusForbidden, gin.H{}, "没有权限访问此操作", ctx)
				ctx.Abort()
				return
			} */
		}
		ctx.Next()
	}
}
